#coding=utf-8

from views.view_base import *
from common.middlewares import *
from conf.constant import *
from controller.user_controller import UserController


# 登陆
class UserView(ViewBase):
    @dispatch('/login', methods=['GET', 'POST'])
    @verify_params(username='string', password='string')
    def login(self):
        code = RESPONSE_FAILED
        data = {}
        msg = '登陆失败'

        try:
            username = self.params.get('username', '')
            password = self.params.get('password', '')

            controller = UserController()
            result = controller.login(username=username, password=password)

            if result.get('token', ''):
                code = RESPONSE_SUCCESS
                data = result
                msg = 'success'
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data=data, msg=msg)



